package webApp.home;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shoeHouse.commodityInfo.model.CommodityInfo;
import com.shoeHouse.commodityInfo.service.CommodifyInfoService;
import com.shoeHouse.order.model.OrderInfo;
import com.shoeHouse.order.service.OrderService;
import com.shoeHouse.orderEvaluate.model.OrderEvaluate;
import com.shoeHouse.orderEvaluate.service.OrderEvaluateService;
import com.shoeHouse.user.model.UserInfo;
import com.shoeHouse.user.service.UserInfoService;
import com.shoeHouse.util.FileUploadUtil;
import com.shoeHouse.util.IdWork;
import com.shoeHouse.util.PageHelper;
import com.shoeHouse.util.ResultInfo;

@Controller
@RequestMapping("/home")
public class HomeAction {
	@Autowired 
	private UserInfoService userInfoService;
	@Autowired
	private CommodifyInfoService commodifyInfoService;
	@Autowired
	private OrderEvaluateService orderEvaluateService;
	@Autowired
	private OrderService orderService;
	
	
	@RequestMapping("/account")
	public String account(String text,Map<String, Object> map){
		try {
			text=new String(text.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map.put("text",text);
		
		
		return "/webApp/commodify/commodifyText.jsp";
		
		
	}
	
	
	
	@RequestMapping("/jingpinShoe")
	public String jingpinShoe(HttpServletRequest request,Map<String, Object> map,int page,int rows){
		PageHelper pageHelper=new PageHelper();
		pageHelper.setCurrentPage(page);
		pageHelper.setPageSize(rows);
		CommodityInfo commodityInfo=new CommodityInfo();
		commodityInfo.setOrderNum("5");
		List<CommodityInfo> list=commodifyInfoService.getAll(request,commodityInfo, pageHelper);
		map.put("allCommodify",list);
		map.put("jingpin","class=\"list-curr\"");
		return "/webApp/homePage/shop.jsp";
		
		
	}
	@RequestMapping("/shouhou")
	public String shouhou(){
		
		
		return "/webApp/PersonalCenter/shouhou.jsp";
	}
	@RequestMapping("/commodifySearch")
	public String commodifySearch(String text,Map<String, Object> map,int page,int rows){
		PageHelper pageHelper=new PageHelper();
		pageHelper.setCurrentPage(page);
		pageHelper.setPageSize(rows);
		try {
			text=new String(text.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<CommodityInfo> list=commodifyInfoService.commodifySearch(text, pageHelper);
		map.put("allCommodify",list);
		map.put("search","class=\"list-curr\"");
		return "/webApp/homePage/shop.jsp";
		
		
		
	}
	@RequestMapping("/comm")
	public String comm(String orderId,String commId,String sellerId,Map<String, Object> map){
		map.put("orderId",orderId);
		map.put("commId",commId);
		map.put("sellerId",sellerId);
		return "/webApp/PersonalCenter/comm.jsp";
	}
	@RequestMapping("/saveComment")
	public String saveComment(OrderEvaluate orderEvaluate,Map<String, Object> map,HttpSession session){
	
		
		try {
			orderEvaluate.setCommodifyEvaluate(new String(orderEvaluate.getCommodifyEvaluate().getBytes("ISO-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		orderEvaluate.setOrderId(orderEvaluate.getOrderId().trim());
		orderEvaluate.setGmtCreate(new Date());
		orderEvaluate.setGmtModified(new Date());
		orderEvaluate.setId(IdWork.nextId());
		//orderEvaluate.setPid("0");
		orderEvaluate.setOrderEvaluateStatus("0");
		UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
		orderEvaluate.setUserId(userInfo.getId());
		orderEvaluateService.save(orderEvaluate);
		OrderInfo orderInfo=new OrderInfo();
		orderInfo.setId(orderEvaluate.getOrderId().trim());
		orderInfo.setPayStatus("完成订单");
	
		orderService.update(orderInfo);
		return "redirect:/home/procomment?commId="+orderEvaluate.getCommodifyId();
		
	}
	@RequestMapping("/procomment")
	public String procomment(HttpServletRequest request,String commId,Map<String, Object> map){
		OrderEvaluate orderEvaluate=new OrderEvaluate();
		PageHelper pageHelper=new PageHelper();
		  pageHelper.setCurrentPage(1);
		  pageHelper.setPageSize(100);
		orderEvaluate.setCommodifyId(commId);
		List<OrderEvaluate> list=orderEvaluateService.getAll(request,orderEvaluate, pageHelper);
		
		map.put("model",list);
		return "/webApp/commodify/procomment.jsp";
		
		
	}
	
	@RequestMapping("/registeredPage")
	public String registeredPage(){
		
		
		
		return "/webApp/homePage/registered.jsp";
	}
	@ResponseBody
	@RequestMapping("/registered")
	public  ResultInfo registered(HttpServletRequest request,UserInfo userInfo,@RequestParam(value = "file", required = false) MultipartFile file){
		
		
		if(file != null){
			try {
				String icon_url = FileUploadUtil.saveFile(file, request,"photo");
				System.out.println(icon_url);
				userInfo.setPicture(icon_url);
			} catch (Exception e) {
				
			}
		}
		ResultInfo info=new ResultInfo();
		userInfo.setId(IdWork.nextId());
		userInfo.setEndLoginTime(new Date());
		userInfo.setGmtCreate(new Date());
		userInfo.setGmtModified(new Date());
		userInfo.setRoleId("5");
		userInfo.setStatus(Byte.valueOf("0"));
		userInfo.setDepartmentId("5");
		
		userInfoService.save(userInfo);
		
		
	return info;
		
		
		
	}
	@RequestMapping("/login")
  public String login(){
	  return "/webApp/login.jsp";
  }
	@ResponseBody
	@RequestMapping("/loginVali")
  public ResultInfo loginVali(String username,String password,HttpSession session){
		UserInfo userInfo=new UserInfo();
		userInfo.setUsername(username);
		//userInfo.setPassword(password);
		
		ResultInfo info=null;
		try {
			UserInfo userInfo2=userInfoService.getSingle(userInfo);
			 info = new ResultInfo();
	
				if(password.equals(userInfo2.getPassword())){
					
						
					  session.setAttribute("userInfo",userInfo2);
			           info.setSuccess(0);
			           info.setMsg("登录成功");
			       
			           return info;
					
				}
				else{
					info.setMsg("密码错误");
				    info.setSuccess(1);
				}
				
				
		
			
			return info;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			info.setMsg("用户名错误");
		    info.setSuccess(2);
			return info;
		}
	 
  }	
	
	@RequestMapping("/main")
  public String main(Map<String, Object> map,HttpSession session,HttpServletRequest request){
		CommodityInfo commodityInfo=new CommodityInfo();
		PageHelper pageHelper=new PageHelper();
		pageHelper.setPageSize(6);
		pageHelper.setCurrentPage(1);
		commodityInfo.setCategoryId("1");
		List<CommodityInfo> list=commodifyInfoService.getAll(request,commodityInfo, pageHelper);
		map.put("grilShoe",list);
		commodityInfo.setCategoryId("2");
		List<CommodityInfo> list2=commodifyInfoService.getAll(request,commodityInfo, pageHelper);
	     map.put("boyShoe",list2);
	     
	     map.put("bottomMenu","class=\"curr\"");
	     try {
			UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
			if(userInfo.getId()!=null){
				map.put("user","user");
				
			}
			
		} catch (Exception e) {
			map.put("user",null);
		}
	  return"/webApp/main.jsp";
	  
  }	
	@RequestMapping("/commodify")
	public String commodify(Map<String, Object> map,String cate,int page,int rows,HttpServletRequest request){
	
		CommodityInfo commodityInfo=new CommodityInfo();
		PageHelper pageHelper=new PageHelper();
		pageHelper.setPageSize(rows);
		pageHelper.setCurrentPage(page);
		String style="style";
		if(cate.equals("boy")){
			commodityInfo.setCategoryId("1");
			map.put("cate","boy");
			style="styleboy";
		}
        if(cate.equals("gril")){
        	map.put("cate","gril");
        	commodityInfo.setCategoryId("2");
        	style="stylegril";
		}
        if(cate.equals("all")){
        	map.put("cate","all");
        	
        }
		List<CommodityInfo> list=commodifyInfoService.getAll(request,commodityInfo, pageHelper);
		map.put("allCommodify",list);
		map.put(style,"class=\"list-curr\"");
		map.put("page",page);
		map.put("bottomMenuComm","class=\"curr\"");
		map.put("count",list.size());
		return "/webApp/homePage/shop.jsp";
		
		
	}
	@RequestMapping("/commodifyContent")
	private String commodifyContent(Map<String, Object> map,String id,int page,int rows){
		CommodityInfo info=new CommodityInfo();
		info.setId(id);
		CommodityInfo commodityInfo=commodifyInfoService.getSingle(info);
		map.put("model",commodityInfo);
		if(commodityInfo.getShoeSize()!=null){
		ArrayList<String> list=new ArrayList<>();
		String[] size=commodityInfo.getShoeSize().split(",");
	
		for(int i=0;i<size.length;i++){
			list.add(size[i]);
			
		}
		map.put("sizeList",list);
		}
		return "/webApp/commodify/commodifyContent.jsp";
		
	}
	
	@RequestMapping("/commodifyBuy")
	public String commodifyBuy(OrderInfo orderInfo,Map<String, Object> map,HttpSession session){
//	    OrderInfo orderInfo=new OrderInfo();
//	    orderInfo.setCommodifyId(id);
		try {
			orderInfo.setAddress(new String(orderInfo.getAddress().getBytes("ISO-8859-1"),"UTF-8"));
			orderInfo.setWay(new String(orderInfo.getWay().getBytes("ISO-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	    orderInfo.setStatus(Byte.parseByte("0"));
	    orderInfo.setId(IdWork.nextId());
	    UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
	    orderInfo.setUserId(userInfo.getId());
	    orderInfo.setGmtCreate(new Date());
	    orderInfo.setGmtModified(new Date());
	    orderInfo.setSerialNumber("12");
	    if(orderInfo.getWay().equals("待付款")){
	    	orderInfo.setPayStatus("未付款");
	    	orderInfo.setDeliverGoodsStatus("未付款");
	    }else{
	    	orderInfo.setPayStatus("待发货");
	    	orderInfo.setDeliverGoodsStatus("待确认");
	    }
	    
	    orderService.save(orderInfo);
	    CommodityInfo commodityInfo=new CommodityInfo();
	    commodityInfo.setId(orderInfo.getCommodifyId());
		CommodityInfo commodityInfo2=commodifyInfoService.getSingle(commodityInfo);
	    int orderNum=Integer.valueOf(commodityInfo2.getOrderNum())+1;
	    int invenNum=commodityInfo2.getInventoryNum()-1;
	    CommodityInfo commodityInfo3=new CommodityInfo();
	    commodityInfo3.setId(orderInfo.getCommodifyId());
	    commodityInfo3.setOrderNum(String.valueOf(orderNum));
	    commodityInfo3.setInventoryNum(invenNum);
	    commodifyInfoService.update(commodityInfo3);
		
		return "redirect:/home/commodifyContent?id="+ orderInfo.getCommodifyId()+"&page=1&rows=10";
		
		
		
	}
	
	@RequestMapping("/OrderContent")
	public String OrderContent(HttpServletRequest request,Map<String, Object> map,String id,int page,int rows){
		
		OrderEvaluate orderEvaluate=new OrderEvaluate();
		PageHelper pageHelper=new PageHelper();
		pageHelper.setPageSize(rows);
		pageHelper.setCurrentPage(page);
		List<OrderEvaluate> list=orderEvaluateService.getAll(request,orderEvaluate, pageHelper);
		map.put("count",list.size());
		map.put("evaluate",list);
		map.put("page",page);
		return null;
	}
	/**
	 * 
	 * 我的订单
	 */
	@RequestMapping(value="/myOrder")
	public String myordel(HttpServletRequest request,Map<String, Object> map,HttpSession session){
		OrderInfo orderInfo=new OrderInfo();
		PageHelper pageHelper=new PageHelper();
		pageHelper.setCurrentPage(1);
		pageHelper.setPageSize(100);
		UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
		System.out.println(userInfo.getId());
		orderInfo.setUserId(userInfo.getId());
	  List<OrderInfo> list=orderService.getAll(request,orderInfo, pageHelper);
		
		map.put("orderInfo",list);
		return "/webApp/PersonalCenter/myorder.jsp";
		
		
		
	}
	/**
	 * 订单详情
	 */
	@RequestMapping(value="/ordercon")
	public String ordercon(String id,HttpServletRequest request,Map<String, Object> map,HttpSession session){
	     OrderInfo orderInfo=new OrderInfo();
	     orderInfo.setId(id);
//	     UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
//		 orderInfo.setUserId(userInfo.getId());
	     OrderInfo orderInfo2=orderService.getSingle(orderInfo);

	     
	     map.put("orderInfos",orderInfo2);
		
		
		return "/webApp/PersonalCenter/ordercon.jsp";
		
	}
	
	/**
	 * 待评价
	 */
	@RequestMapping(value="/waitComm")
	public String waitComm(HttpServletRequest request,Map<String, Object> map,HttpSession session){
		  OrderInfo orderInfo=new OrderInfo();
		  PageHelper pageHelper=new PageHelper();
		  pageHelper.setCurrentPage(1);
		  pageHelper.setPageSize(100);
		  orderInfo.setPayStatus("待评价");
		  UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
			 orderInfo.setUserId(userInfo.getId());
		List<OrderInfo> list=orderService.getAll(request,orderInfo, pageHelper);

		map.put("model",list);
		return "/webApp/PersonalCenter/waitComm.jsp";
	}
	@RequestMapping("/pay")
	public String pay(OrderInfo orderInfo){
		orderInfo.setWay("货到付款");
		orderInfo.setPayStatus("待发货");
		 orderInfo.setDeliverGoodsStatus("待确认");
		orderService.update(orderInfo);
		
		
		return "redirect:/home/waitPay";
		
		
	}
	/**
	 * 待付款
	 */
	@RequestMapping(value="/waitPay")
	public String waitPay(HttpServletRequest request,Map<String, Object> map,HttpSession session){
		System.out.println("待付款");
		 OrderInfo orderInfo=new OrderInfo();
		  PageHelper pageHelper=new PageHelper();
		  pageHelper.setCurrentPage(1);
		  pageHelper.setPageSize(100);
		  orderInfo.setPayStatus("待付款");
		  UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
			 orderInfo.setUserId(userInfo.getId());
		List<OrderInfo> list=orderService.getAll(request,orderInfo, pageHelper);
		map.put("model",list);
		return "/webApp/PersonalCenter/waitPay.jsp";
		
		
		
		
	}
	/**
	 * 待发货
	 */
	@RequestMapping(value="/waitCargo")
	public String waitCargo(HttpServletRequest request,Map<String, Object> map,HttpSession session){
		 OrderInfo orderInfo=new OrderInfo();
		  PageHelper pageHelper=new PageHelper();
		  pageHelper.setCurrentPage(1);
		  pageHelper.setPageSize(100);
		  orderInfo.setPayStatus("待发货");
		  UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
			 orderInfo.setUserId(userInfo.getId());
		List<OrderInfo> list=orderService.getAll(request,orderInfo, pageHelper);
		map.put("model",list);
		return "/webApp/PersonalCenter/waitCargo.jsp";
	
	
	}
	/**
	 * 待收货
	 */
	@RequestMapping(value="/waitTheGoods")
	public String waitTheGoods(HttpServletRequest request,Map<String, Object> map,HttpSession session){
		
		 OrderInfo orderInfo=new OrderInfo();
		  PageHelper pageHelper=new PageHelper();
		  pageHelper.setCurrentPage(1);
		  pageHelper.setPageSize(100);
		  orderInfo.setPayStatus("待收货");
		  UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
			 orderInfo.setUserId(userInfo.getId());
		List<OrderInfo> list=orderService.getAll(request,orderInfo, pageHelper);
		map.put("model",list);
		
		return "/webApp/PersonalCenter/waitTheGoods.jsp";
		
		
		
	
	}
	@RequestMapping("/confirm")
	public String confirm(String id){
		OrderInfo orderInfo=new OrderInfo();
		orderInfo.setId(id);
		orderInfo.setDeliverGoodsStatus("已送到");
		orderInfo.setPayStatus("待评价");
		orderService.update(orderInfo);
		return "redirect:/home/waitTheGoods";
		
		
	}
	
	
}
